package cn.edu.fzu.homemaking.controller;

import cn.edu.fzu.homemaking.api.OrderApi;
import cn.edu.fzu.homemaking.api.SsoApi;
import cn.edu.fzu.homemaking.api.WalletApi;
import cn.edu.fzu.homemaking.common.SessionConstant;
import cn.edu.fzu.homemaking.domain.entity.Order;
import cn.edu.fzu.homemaking.domain.enums.OrderStatusEnum;
import cn.edu.fzu.homemaking.helper.QRCodeHelper;
import cn.edu.fzu.homemaking.util.ResultUtil;
import cn.edu.fzu.homemaking.web.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.binary.Base64;
import org.springframework.util.Assert;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.math.BigDecimal;
import java.util.Objects;

@Api(tags = "用户钱包信息管理")
@RequestMapping("/api/user/auth/wallet")
@RestController
public class WalletController {

    private static final String RECHARGE_URL = "http://101.43.44.19:8200/api/wallet/recharge?money=";

    private static final String PAY_DESC     = "订单消费，订单号为：";

    @Resource
    private OrderApi            orderApi;

    @Resource
    private WalletApi           walletApi;

    @Resource
    private SsoApi              ssoApi;

    @ApiOperation(value = "获取本人账户金额", httpMethod = "GET")
    @GetMapping("/getWalletInfo")
    public Result getWalletInfo() {
        return walletApi.getWallet();
    }


    @ApiOperation(value = "获取本人账户资金流水", httpMethod = "GET")
    @GetMapping("/getWater")
    public Result getWater(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        return walletApi.getWater(pageNum, pageSize);
    }


    @ApiOperation(value = "查看转账流水详情")
    @GetMapping("/getWaterDetail")
    public Result getWaterDetail(Long id) {
        return walletApi.getWaterDetail(id);
    }


    @ApiOperation(value = "充值")
    @ApiImplicitParam(name = "money", value = "充值金额", required = true)
    @GetMapping("/recharge")
    public Result recharge(BigDecimal money, HttpServletRequest request) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        QRCodeHelper.buildQRCodeToBufferedImage(160, 160,
                RECHARGE_URL + money + "&" + SessionConstant.DSESSIONID + "=" + request.getSession().getId(),
                outputStream);
        return new Result(Base64Utils.encodeToString(outputStream.toByteArray()));
    }


    @ApiOperation(value = "是否充值成功")
    @ApiImplicitParam(name = "money", value = "充值金额", required = true)
    @GetMapping("/isRecharge")
    public Result isRecharge(BigDecimal money) {
        BigDecimal m = new BigDecimal(ssoApi.getSession(SessionConstant.RECHARGE).getValue().toString());
        return new Result(Objects.equals(m, money));
    }


    @ApiOperation(value = "支付")
    @ApiImplicitParams({ @ApiImplicitParam(name = "orderId", required = true, value = "订单id") })
    @PostMapping("/pay")
    public Result transfer(String orderId) {
        Order order = ResultUtil.parseValue(orderApi.selectByOrderId(orderId), Order.class);
        Assert.isTrue(order != null && order.getStatus() == OrderStatusEnum.PAYING, "订单不存在或该订单目前无需支付");
        BigDecimal money = order.getPrice().multiply(BigDecimal.valueOf(order.getQty()));
        walletApi.transfer(order.getCompanyId(), money, PAY_DESC + orderId);
        orderApi.updateStatus(orderId, OrderStatusEnum.ENDED);
        return Result.SUCCESS;
    }

}
